package com.ruoyi.web.controller.basicInformation;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.basicInformation.domain.TServiceDeployment;
import com.ruoyi.system.basicInformation.vo.TServiceDeploymentTwoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.basicInformation.vo.TServiceDeploymentVo;
import com.ruoyi.system.basicInformation.bo.TServiceDeploymentQueryBo;
import com.ruoyi.system.basicInformation.bo.TServiceDeploymentAddBo;
import com.ruoyi.system.basicInformation.bo.TServiceDeploymentEditBo;
import com.ruoyi.system.basicInformation.service.ITServiceDeploymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 勤务部署基本信息Controller
 *
 * @author ruoyi
 * @date 2021-05-20
 */
@Api(value = "勤务部署基本信息控制器", tags = {"勤务部署基本信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/basicInformation/deployment")
public class TServiceDeploymentController extends BaseController {

    private final ITServiceDeploymentService iTServiceDeploymentService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询勤务部署基本信息列表
     */
    @ApiOperation("查询勤务部署基本信息列表")
//    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:list')")
    @GetMapping("/list")
    public TableDataInfo<TServiceDeploymentVo> list(TServiceDeploymentQueryBo bo) {
        startPage();
        List<TServiceDeploymentVo> list = iTServiceDeploymentService.queryList(bo);
        return getDataTable(list);
    }

    @Log(title = "船舶信息管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basicInformation:ship:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TServiceDeployment> util = new ExcelUtil<TServiceDeployment>(TServiceDeployment.class);
        List<TServiceDeployment> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = iTServiceDeploymentService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TServiceDeployment> util = new ExcelUtil<TServiceDeployment>(TServiceDeployment.class);
        return util.importTemplateExcel("勤务部署数据");
    }

    /**
     * 导出勤务部署基本信息列表
     */
    @ApiOperation("导出勤务部署基本信息列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:export')")
    @Log(title = "勤务部署基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<T> export(TServiceDeploymentQueryBo bo) {
        if ("1".equals(bo.getExportType())) {
            List<TServiceDeploymentVo> list = iTServiceDeploymentService.queryList(bo);
            ExcelUtil<TServiceDeploymentVo> util = new ExcelUtil<TServiceDeploymentVo>(TServiceDeploymentVo.class);
            return util.exportExcel(list, "勤务部署基本信息");
        } else {
            List<TServiceDeploymentTwoVo> list = iTServiceDeploymentService.queryListTwo(bo);
            ExcelUtil<TServiceDeploymentTwoVo> util = new ExcelUtil<TServiceDeploymentTwoVo>(TServiceDeploymentTwoVo.class);
            return util.exportExcel(list, "信息登记基本信息");
        }
    }

    /**
     * 获取勤务部署基本信息详细信息
     */
    @ApiOperation("获取勤务部署基本信息详细信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:query')")
    @GetMapping("/{id}")
    public AjaxResult<TServiceDeploymentVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTServiceDeploymentService.queryById(id));
    }

    /**
     * 新增勤务部署基本信息
     */
    @ApiOperation("新增勤务部署基本信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:add')")
    @Log(title = "勤务部署基本信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TServiceDeploymentAddBo bo) {
        if ("1".equals(iTServiceDeploymentService.checkNameUnique(bo.getName(), 0))) {
            return AjaxResult.error("新增勤务部署：'" + bo.getName() + "'失败，已存在");
        }
        return toAjax(iTServiceDeploymentService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改勤务部署基本信息
     */
    @ApiOperation("修改勤务部署基本信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:edit')")
    @Log(title = "勤务部署基本信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TServiceDeploymentEditBo bo) {
        if ("1".equals(iTServiceDeploymentService.checkNameUnique(bo.getName(), bo.getId().intValue()))) {
            return AjaxResult.error("修改勤务部署：'" + bo.getName() + "'失败，已存在");
        }
        return toAjax(iTServiceDeploymentService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 完善勤务部署基本信息
     */
    @ApiOperation("修改勤务部署基本信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:edit')")
    @Log(title = "勤务部署基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editExt")
    public AjaxResult<Void> editExt(@RequestBody TServiceDeploymentEditBo bo) {
        return toAjax(iTServiceDeploymentService.updateByEditBoExt(bo) ? 1 : 0);
    }

    /**
     * 删除勤务部署基本信息
     */
    @ApiOperation("删除勤务部署基本信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:deployment:remove')")
    @Log(title = "勤务部署基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTServiceDeploymentService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
